<?php

/**
 * Add a relationship
 * @param int $guid_one
 * @param string $relationship (Use predefined constant for relationship type)
 * @param int $guid_two
 * @param (int|NULL) $time_crated Default is NULL
 * @author Pablo
 * @since 1.0
 */
function blg_add_relationship($guid_one, $relationship, $guid_two, $time_created=NULL){
	if(empty($time_crated))
		$time_created= time();

	$query= blg_dbschema_query_buid(blg_dbschema_insert_relationship(array(
		'guid_one' => $guid_one,
		'relationship' => $relationship,
		'guid_two' => $guid_two,
		'time_created' => $time_created
	)));
	$result= blg_dbschema_insert($query);
	
	if($result->success){
		return $result->new_id;
	}
	
	return FALSE;
}

/**
 * Deletes a relationship by id
 * @param in $id
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_delete_relationship($id){
	$query= blg_dbschema_query_buid(blg_dbschema_delete_relationships(array('id' => $id)));
	$result= blg_dbschema_delete($query);
	return ($result->success && $result->affected_rows>0);
}

/**
 * Deletes all entity's relationships (NOTE: guid_one OR guid_two)
 * @param int $entity_guid
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_delete_entity_relationships($entity_guid){
	$query= blg_dbschema_query_buid(blg_dbschema_delete_relationships(array('guid' => $entity_guid)));
	$result= blg_dbschema_delete($query);
	return ($result->success && $result->affected_rows>0);	
}

/**
 * Deletes all entity's relationships (NOTE: ONLY guid_one)
 * @param int $entity_guid
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_delete_entity_relationships_one($entity_guid){
	$query= blg_dbschema_query_buid(blg_dbschema_delete_relationships(array('guid_one' => $entity_guid)));
	$result= blg_dbschema_delete($query);
	return ($result->success && $result->affected_rows>0);		
}

/**
 * Deletes all entity's relationships (NOTE: ONLY guid_two)
 * @param int $entity_guid
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_delete_entity_relationships_two($entity_guid){
	$query= blg_dbschema_query_buid(blg_dbschema_delete_relationships(array('guid_two' => $entity_guid)));
	$result= blg_dbschema_delete($query);
	return ($result->success && $result->affected_rows>0);		
}

/**
 * Get entity relationships (NOTE: guid_one OR guid_two)
 * @param int $entity_guid
 * @param string $relationship (Use predefined constant for relationship type)
 * @return mixed
 * @author Pablo
 * @since 1.0
 */
function blg_get_relationships($entity_guid, $relationship){
	$query= blg_dbschema_query_buid(blg_dbschema_select_relationships(array(
		'guid' => $entity_guid,
		'relationship' => $relationship
	)));
	$result= blg_dbschema_select($query);
	
	if($result->success){
		return $result;
	}
	
	return FALSE;
}

/**
 * Get entity relationships (NOTE: guid_one OR guid_two)
 * @param int $guid_one
 * @param string $relationship (Use predefined constant for relationship type)
 * @param int $guid_two
 * @return mixed
 * @author Pablo
 * @since 1.0
 */
function blg_get_relationship($guid_one, $relationship, $guid_two){
	$query= blg_dbschema_query_buid(blg_dbschema_select_relationships(array(
		'guid_one' => $guid_one,
		'relationship' => $relationship,
		'guid_two' => $guid_two
	)));
	$result= blg_dbschema_select($query);
	
	if($result->success && $result->num_rows==1){
		return $result->{0};
	}
	
	return FALSE;
}

